package com.lohanry.Instance;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.media.RingtoneManager;
import android.os.Build;
import android.util.Log;

/**
 * Created by Lohanry on 2017/2/15.
 */

public class ExampleNotificationAndBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        String ticker = intent.getStringExtra("ticker");
        String title = intent.getStringExtra("title");
        String message = intent.getStringExtra("message");
        String s_icon = intent.getStringExtra("s_icon");
        String l_icon = intent.getStringExtra("l_icon");
        int color = intent.getIntExtra("color", 0);
        String unityClass = intent.getStringExtra("activity");
        Boolean sound = intent.getBooleanExtra("sound", false);
        Boolean vibrate = intent.getBooleanExtra("vibrate", false);
        Boolean lights = intent.getBooleanExtra("lights", false);
        int id = intent.getIntExtra("id", 0);
        Resources res = context.getResources();
        Class<?> unityClassActivity = null;
        try
        {
            unityClassActivity = Class.forName(unityClass);
        } catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        Log.i("Unity",unityClassActivity.getName());
        Intent notificationIntent = new Intent(Intent.ACTION_MAIN);
        notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        notificationIntent.setClass(context, unityClassActivity);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
        Notification.Builder builder = new Notification.Builder(context);
        builder.setContentIntent(contentIntent)
                .setWhen(System.currentTimeMillis())
                .setAutoCancel(true)
                .setContentTitle(title)
                .setContentText(message);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
            builder.setColor(color);
        if(ticker != null && ticker.length() > 0) {
            builder.setTicker(ticker);
        }
        if (s_icon != null && s_icon.length() > 0) {
            builder.setSmallIcon(res.getIdentifier(s_icon, "drawable", context.getPackageName()));
        }
        if (l_icon != null && l_icon.length() > 0) {
            builder.setLargeIcon(BitmapFactory.decodeResource(res, res.getIdentifier(l_icon, "drawable", context.getPackageName())));
        }
        if(sound) {
            builder.setSound(RingtoneManager.getDefaultUri(2));
        }
        if(vibrate) {
            builder.setVibrate(new long[]{
                    1000L, 1000L
            });
        }
        if(lights) {
            builder.setLights(Color.GREEN, 3000, 3000);
        }
        Notification notification = builder.build();
        notificationManager.notify(id, notification);
    }
}
